<?php
class OrderModel extends Model{
	private $table1 = 'order';
	private $table2 = 'products';
	private $table3 = 'order_status';
	private $table4 = 'membership';
	
	//视图
	private $table5 = 'orderdetail';
	
	/**
	 * 获取订单详细信息
	 */
	public function Orderdetail($where){
		/*  $sql = "SELECT o.*,p.products_name,p.contact_person,p.contact_tel,p.products_type,os.name as os_name FROM `".$this->table1."` o LEFT JOIN ".$this->table2." p ON o.products_id=p.products_id LEFT JOIN ".
				 $this->table3." os ON o.order_status_id=os.order_status_id WHERE o.order_id=".$where.";";
		 $result = M($this->table1)->query($sql);
		// echo $sql;die;
		 //echo M($this->table1)->getLastSql();die;
		 return $result[0]; */
		return M($this->table5)->where($where)->find();
		//echo M($this->table5)->getLastSql()
	}
	
	/**
	 * 获取会员订单列表
	 * @param $where $order $limit $field
	 */
	public function getMemberOrder($where,$order,$field){
		 //return $this->table("`".$this->table1."` o")->join($this->table2." p ON o.products_id=p.products_id")->join($this->table3." os ON o.order_status_id=os.order_status_id")->where($where)->field($field)->order($order)->select();
		 return M($this->table5)->where($where)->field($field)->order($order)->select();
		 //echo M($this->table5)->getLastSql();die;
	}
	
	/**
	 * 后台获取订单列表
	 * @param $where $order $limit $field
	 * @return array
	 */
	public function getOrderList($where,$order,$limit,$field){
		$list = M($this->table5)->where($where)->order($order)->field($field)->limit($limit)->select();//$this->table("`".$this->table1."` o")->join($this->table2." p ON o.products_id=p.products_id")->join($this->table3." os ON o.order_status_id=os.order_status_id")->where($where)->field($field)->order($order)->limit($limit)->select();
		//echo  $this->getLastSql();die;
		foreach($list as $key=>$val){
			$bdetail = M($this->table4)->where(array('uid'=>$val['uid']))->field('name,tel')->find();
			$sdetail = M($this->table4)->where(array('uid'=>$val['publish_uid']))->field('name,tel')->find();
			$list[$key]['bdetail']=$bdetail;
			$list[$key]['sdetail']=$sdetail;
		}
		return $list;
	}
	
	/**
	 * 后台获取订单个数
	 * @param $where
	 * @return array
	 */
	public function getOrderCount($where){
		return  M($this->table5)->where($where)->count();//$this->table("`".$this->table1."` o")->join($this->table2." p ON o.products_id=p.products_id")->join($this->table3." os ON o.order_status_id=os.order_status_id")->where($where)->count();
	}
	
	/**
	 * 后台获取订单详情
	 * @param $where $field
	 */
	public function getOrderDetail($where,$field){
		//$this->table("`".$this->table1."` o")->join($this->table2." p ON o.products_id=p.products_id")->join($this->table3." os ON o.order_status_id=os.order_status_id")->where($where)->field($field)->find();
		return M($this->table5)->where($where)->field($field)->find();
	}
	
	/**
	 * 按照天数获取产品个数
	 * @param string $where
	 */
	public function getOcByday($where=1,$limit,$order){
		if($limit){
			$sql = "select DATE_FORMAT(o.order_date_added,'%Y-%m-%d') day, count(*) c from `".$this->table1."` o LEFT JOIN ".$this->table2." p ON o.products_id=p.products_id WHERE ".$where." group by DATE_FORMAT(o.order_date_added,'%Y%m%d') ORDER BY ".$order." LIMIT ".$limit;
		}else{
			$sql = "select DATE_FORMAT(o.order_date_added,'%Y-%m-%d') day, count(*) c from `".$this->table1."` o LEFT JOIN ".$this->table2." p ON o.products_id=p.products_id WHERE ".$where." group by DATE_FORMAT(o.order_date_added,'%Y%m%d') ORDER BY ".$order;
		}
		//echo $sql;die;
		return M($this->table1)->query($sql);
	}
	
	
	/**
	 * 获取按天数获取产品的个数
	 * @param string  $where
	 */
	public function getOc($where=1){
		$sql = "select DATE_FORMAT(o.order_date_added,'%Y-%m-%d') day, count(*) c from `".$this->table1."` o LEFT JOIN ".$this->table2." p ON o.products_id=p.products_id WHERE ".$where." group by DATE_FORMAT(o.order_date_added,'%Y%m%d') ";
		$result = M($this->table1)->query($sql);
		return count($result);
	}

	/**
	 * 按照天数获取产品个数
	 * @param string $where
	 */
	public function getOcDetail($where=1,$limit,$order){
		if($limit){
			$sql = "select o.*,p.products_name,os.name as os_name,m.name as bname,m.tel as btel  from `".$this->table1."` o LEFT JOIN ".$this->table2." p ON o.products_id=p.products_id LEFT JOIN ".$this->table3." os ON o.order_status_id=os.order_status_id LEFT JOIN ".$this->table4." m ON o.uid=m.uid WHERE ".$where." ORDER BY ".$order." LIMIT ".$limit;
		}else{
			$sql = "select o.*,p.products_name,os.name as os_name,m.name as bname,m.tel as btel  from `".$this->table1."` o LEFT JOIN ".$this->table2." p ON o.products_id=p.products_id LEFT JOIN ".$this->table3." os ON o.order_status_id=os.order_status_id LEFT JOIN ".$this->table4." m ON o.uid=m.uid WHERE ".$where." ORDER BY ".$order;
		}
		//echo $sql;die;
		$result = M($this->table1)->query($sql);
		foreach($result as $key=>$val){
			$sresult = M($this->table4)->where(array('uid'=>$val['publish_uid']))->field('name,tel')->find();
			$result[$key]['sdetail'] = $sresult;
		}
		return $result;
	}
}